﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Grocery
{
    class CashierQueue
    {
        private int UnitServiceMinutes = 1; //default
        private Queue<Customer> queue;
        private Customer currentCustomer;
        private int Mintes = 1;

        public CashierQueue(int number, int unit)
        {
            Number = number;
            UnitServiceMinutes = unit;
            queue = new Queue<Customer>();
        }

        internal int Number
        {
            private set;
            get;
        }

        internal void AppendCustomer(Customer customer)
        {
            lock (queue)
            {
                queue.Enqueue(customer);
            }
        }

        internal bool IsEmpty
        {
            get
            {
                lock (queue)
                {
                    return  queue.Count == 0 && CompleteCurrent;
                }
            }
        }

        internal int Count
        {
            get
            {
                int count = queue.Count;
                if (currentCustomer != null && !currentCustomer.Finished)
                {
                    ++count;
                }
                return count;
            }
        }

        private bool CompleteCurrent
        {
            get
            {
                return currentCustomer == null || currentCustomer.Finished;
            }
        }

        internal int LastOne
        {
            get
            {
                if (Count == 0)
                {
                    return 0;
                }
                else
                {
                    if (Count == 1 && !CompleteCurrent)
                    {
                        return currentCustomer.Items;
                    }
                    else
                    {
                        lock (queue)
                        {
                            return queue.Last<Customer>().Items;
                        }
                    }
                }
            }
        }

        internal void Service()
        {
            ++Mintes;
            while (currentCustomer == null || currentCustomer.Finished)
            {
                lock (queue)
                {
                    if (queue.Count == 0)
                    {
                        //empty queue do nothing
                        break;
                    }
                    else
                    {
                        currentCustomer = queue.Dequeue();
                    }
                }
            }

            if (currentCustomer != null && !currentCustomer.Finished)
            {
                currentCustomer.Service((float)1.0 / UnitServiceMinutes);
            }

            if (currentCustomer != null && currentCustomer.Finished)
            {
                DebugInfo.Ouput("t=" + Mintes.ToString()
                    + ", Customer #" + Number.ToString()
                    + " Type: " + currentCustomer.customerType.ToString()
                    + " with items:" + currentCustomer.Items.ToString() 
                    + " Finished at Queue:" + Number.ToString());

                currentCustomer = null;
            }
        }
    }
}
